package electionCtrl;

import javax.xml.stream.XMLStreamException;

import xml.MockVotingXMLReader;
import xml.MockVotingXMLWriter;

/**
 * A class which serves to make the major manipulations in the Election class
 **/
public class Admin implements Comparable<Admin>
{

	//The username and password of the admin.
	private String password;
	private String username;
	
	private boolean canEndElection;

	/**
	 * Admin constructor, requiring password and username
	 * @param pass the password for the new Admin
	 * @param un the username for the new Admin
	 **/
	public Admin(String un, String pass) {
		this.password = pass;
		this.username = un;
		this.canEndElection = true;
	}
	
	public Admin(String[] userPass)
	{
		this(userPass[0], userPass[1]);
	}
	
	public Admin(MockVotingXMLReader reader) throws XMLStreamException {
		String next = reader.next();
		String usernameIn = "";
		String passwordIn = "";
		
		while (!next.equals("</Admin>")) {
			next = reader.next();
			if (next.equals("<Username>")) {
			next = reader.next();
				while (!next.equals("</Username>")) {
					usernameIn += next;
					next = reader.next();
				}
			}
			
			if (next.equals("<Password>")) {
			next = reader.next();
				while (!next.equals("</Password>")) {
					passwordIn += next;
					next = reader.next();
				}
			}
		}
		
		username = usernameIn;
		password = passwordIn;
	}

	/**
	 * Accessor method for Admin username
	 * @return the Admin's username
	 **/
	public boolean checkUsername(String username) {
		return this.username.equals(username);
	}
	
	/**
	 * Accessor method for Admin password
	 * @return the Admin's password
	 **/
	public boolean checkPassword(String password) {
		return this.password.equals(password);
	}

	/**
	 * Mutator method for Admin password
	 * @param pw the new password
	 **/
	public void setPassword(String pw) {
		password = pw;
	}

	/**
	 * Mutator method for Admin username
	 * @param pw the new username
	 **/
	public void setUsername(String un) {
		username = un;
	}
	
	public boolean checkUsername(Admin a)
	{
		return this.username.equals(a.username);
	}

	/**
	 * Admins can, legally, create a new administrator.
	 * This method allows such an action.
	 * @param pass the password of the new admin
	 * @param un the username of the new admin
	 * @return the newly constructed Admin
	 **/
	public Admin makeAdmin(String pass, String un) {
		return new Admin(pass, un);
	}

	public boolean canEndElection()
	{
		return this.canEndElection;
	}
	
	public boolean equals(Object o)
	{
		if(!(o instanceof Admin))
		{
			return false;
		}
		Admin a = (Admin)o;
		return this.username.equals(a.username) && this.password.equals(a.password);
	}

	public int compareTo(Admin o)
	{
		return this.username.compareTo(o.username);
	}
	
	public void writeToXML(MockVotingXMLWriter writer) throws XMLStreamException {
		writer.writeStartElement("Admin");
		writer.pushHierarchy();
		writer.writeNewline();
		writer.writeTag("Username",username);
		writer.writeTag("Password",password);
		writer.pullHierarchy();
		writer.writeEndElement();
	}

}